package cn.edu.zut.cs.controller;
import cn.edu.zut.cs.pojo.User;
import cn.edu.zut.cs.service.UserService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

@Controller
public class LoginController {

    @Resource
    private UserService userService;



    /**
     * 登录功能
     */

    @PostMapping("/login")
    public String login(HttpSession session, String username, String userpassword, Model model){

        QueryWrapper<User> wrapper=new QueryWrapper<>();
        wrapper.eq("username",username).eq("userpassword",userpassword);
        User user = userService.getOne(wrapper);
        if (user!=null){
            session.setAttribute("loginUser",user);
            return "home";
        }else {
            model.addAttribute("msg","账号或密码错误，请重新输入");
            return "index";
        }
    }

}
